<?php

namespace App\Console\Commands;

use App\Models\ActivityConfig;
use App\Models\Media;
use App\Models\MediaLikeStatic;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class init_cron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'init_cron:send {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 获取参数，调用方法
        $action = $this->argument('action');
        $this->$action();
    }

    public function media_like()
    {
        $task_key = config('feild.redis.task.init_cron.key').':media_like';
        $max_lock_time = config('feild.redis.task.init_cron.max_lock_time');

        // 检测任务是否正在执行
        if(!empty(Redis::get($task_key)))
            return true;
        Redis::setex($task_key, $max_lock_time, 1);

        $error = [];
        // 点赞任务
        $activity_confit_rows = ActivityConfig::where([['activity_type', '=', 2]])->orderBy('id', 'desc')->first();
        if(empty($activity_confit_rows))
            $error[] = '活动任务不存在';

        // 活动状态
        if(empty($error)){
            if($activity_confit_rows->status != 1)
                $error[] = '活动已结束';
            if($activity_confit_rows->start_time > time())
                $error[] = '活动未开始';
            if($activity_confit_rows->end_time < time())
                $error[] = '活动已结束';
        }

        //分页初始化数据
        if(empty($error)){
            $media_model = new Media();
            $sql = "SELECT count(DISTINCT(uid)) as total_count FROM ".$media_model->getTable()." where status=1 and is_delete=0 and is_private=0 and is_participate_activity=1  ORDER BY created_at asc";
            $result = DB::select($sql);
            if(!empty($result) && $result[0]->total_count > 0){
                $pagesize = 100;
                $total_count = $result[0]->total_count;
                $total_page = ceil($total_count/$pagesize);

                // 分页遍历
                for($i=0;$i<$total_page;$i++){
                    $start_limit = $i*$pagesize;
                    $sel_sql = "SELECT DISTINCT(uid) as uid FROM ".$media_model->getTable()." where status=1 and is_delete=0 and is_private=0 and is_participate_activity=1  ORDER BY created_at asc limit {$start_limit},{$pagesize}";
                    $sel_result = DB::select($sel_sql);
                    if(!empty($sel_result)){
                        foreach ($sel_result as $k=>$v){
                            $uid = $v->uid;

                            // 当前周数据是否存在
                            $media_like_static_rows = MediaLikeStatic::where([['type', '=', 1], ['date_format', '=', date("W", time())], ['uid', '=', $uid]])->first();
                            if(empty($media_like_static_rows)){
                                unset($model);
                                $model = new MediaLikeStatic();
                                $model->activity_id = $activity_confit_rows->id;
                                $model->type = 1;
                                $model->date_format = date("W", time());
                                $model->uid = $uid;
                                $model->create_time = time();
                                $model->update_time = time();
                                $model->save();
                            }

                            // 当前月数据是否存在
                            $media_like_static_rows = MediaLikeStatic::where([['type', '=', 2], ['date_format', '=', date("Ym", time())], ['uid', '=', $uid]])->first();
                            if(empty($media_like_static_rows)){
                                unset($model);
                                $model = new MediaLikeStatic();
                                $model->activity_id = $activity_confit_rows->id;
                                $model->type = 2;
                                $model->date_format = date("Ym", time());
                                $model->uid = $uid;
                                $model->create_time = time();
                                $model->update_time = time();
                                $model->save();
                            }
                        }
                    }
                }// 分页遍历
            }
        }

        // 解锁
        Redis::del($task_key);
    }
}
